//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls
{
	/// <summary>
	/// Summary description for ForumPager.
	/// </summary>
	public class ForumPager : CommunityServer.Controls.Pager
	{

		public override int PageSize 
		{
			get 
			{
				int pageSize = Convert.ToInt32(ViewState["PageSize"]);

				if (pageSize == 0) 
				{
					if (csContext.PostID > 0) 
					{
						return ForumConfiguration.Instance().PostsPerPage;
					} 
					else 
					{
						return ForumConfiguration.Instance().ThreadsPerPage;
					}
				}

				return pageSize;
			}
			set 
			{
				ViewState["PageSize"] = value;
			}

		}

		int _pageIndex = 0;
		public override int PageIndex 
		{
			get 
			{
				// Give first try to the ViewState if it was a postback
				if (Page.IsPostBack && ViewState["PageIndex"] != null)
					_pageIndex = (int) ViewState["PageIndex"];
				else 
				{
					if (csContext.QueryString["pageindex"] != null)
						_pageIndex = int.Parse(csContext.QueryString["pageindex"]) - 1;
					else if(csContext.QueryString["PermaPostID"] != null)
					{
						if(Context.Request.Cookies["PostSortOrder" + csContext.PostID] != null)
							_pageIndex = Posts.GetPageIndex(int.Parse(csContext.QueryString["PermaPostID"]), this.PageSize, 0, int.Parse(Context.Request.Cookies["PostSortOrder" + csContext.PostID].Value)) - 1;
						else
							_pageIndex = Posts.GetPageIndex(int.Parse(csContext.QueryString["PermaPostID"]), this.PageSize, 0, (int)csContext.User.PostSortOrder) - 1;
					}
				}

				if (_pageIndex < 0)
					return 0;
				else
					return _pageIndex;
			}
			set 
			{
				ViewState["PageIndex"] = value;
				_pageIndex = value;
			}
		}

	}

	public class ForumLinkButtonPager : CommunityServer.Controls.LinkButtonPager
	{

		public override int PageSize 
		{
			get 
			{
				int pageSize = Convert.ToInt32(ViewState["PageSize"]);

				if (pageSize == 0) 
				{
					if (CSContext.Current.PostID > 0) 
					{
						return ForumConfiguration.Instance().PostsPerPage;
					} 
					else 
					{
						return ForumConfiguration.Instance().ThreadsPerPage;
					}
				}

				return pageSize;
			}
			set 
			{
				ViewState["PageSize"] = value;
			}

		}

	}
}
